package com.kedll.library.addimg;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.GridView;

import com.kedll.library.R;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.contants.OnItemClickListener;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.AlbumHelper;
import com.kedll.library.util.MyMessageQueue;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.Parse;
import com.kedll.library.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author QiQi
 */
public class AddImgFragmentActivity01 extends MyBaseFragmentActivity implements
        OnItemClickListener {

    /**
     * 控件相关
     */
    private GridView gvImgList;// 相册展示列表

    /**
     * 数据相关
     */
    private AlbumHelper helper;// 图片集帮助类
    private List<Map<String, Object>> albumList;// 图片集集合
    private ArrayList<Map<String, Object>> selectedList;// 已选图片集合
    private int maxImg;// 图片选择最大张数

    private int width;// 屏幕宽
    private int viewWidth;// 适配器内部控件宽

    /**
     * 适配器
     */
    private AddImgAdapter01 albumAdapter;// 相册适配器

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        width = getWindowManager().getDefaultDisplay().getWidth();
        viewWidth = (width - MyUtils.dp2px(
                (float) (8.5 * 2 + 8))) / 2;
        helper = AlbumHelper.getInstance();
        helper.init(getApplicationContext());
        maxImg = getIntent().getIntExtra("maxImg", 1);
        selectedList = (ArrayList<Map<String, Object>>) getIntent()
                .getSerializableExtra("selectedList");
        if (selectedList != null) {
            for (int i = 0; i < selectedList.size(); i++) {
                if (Parse.getInstance().parseBool(selectedList.get(i).get("isAdd"))) {
                    selectedList.remove(i--);
                }
            }
        } else {
            selectedList = new ArrayList<>();
        }
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle(getString(R.string.select_album));
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        helper.onDestroy();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragmentactivity_add_img01;
    }

    @Override
    protected void initEvent() {
        gvImgList = findViewById(R.id.gv_img_list);

        getData();
    }

    @Override
    protected void handlerMessage(Message msg) {
        switch (msg.what) {
            case MyMessageQueue.OK:
                if (albumList != null && albumList.size() != 0) {
                    setAdapter();
                    break;
                }
                ToastUtil.showToast("暂无本地图片");
                break;

            default:
                break;
        }
    }

    /**
     * 设置适配器
     */
    private void setAdapter() {
        if (gvImgList.getAdapter() == null) {
            if (albumAdapter == null) {
                albumAdapter = new AddImgAdapter01(getApplicationContext(), viewWidth);
                albumAdapter.addAll(albumList);
                albumAdapter.setOnItemClickListener(this);
            } else {
                albumAdapter.addAll(albumList);
            }
            gvImgList.setAdapter(albumAdapter);
        } else {
            ((AddImgAdapter01) gvImgList.getAdapter()).addAll(albumList);
        }
    }

    protected void getData() {

        new Thread() {
            @Override
            public void run() {
                albumList = helper.getImagesBucketList(false);
                if (albumList != null) {
                    for (int i = 0; i < albumList.size(); i++) {
                        if (albumList.get(i) == null) {
                            albumList.remove(i--);
                        }
                    }
                }
                if (handler != null) {
                    handler.sendEmptyMessage(MyMessageQueue.OK);
                }
            }
        }.start();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        }
        return super.onKeyDown(keyCode, event);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
        if (arg0 == RESULT_FIRST_USER) {
            if (arg1 == RESULT_OK) {
                selectedList = (ArrayList<Map<String, Object>>) arg2.getSerializableExtra("selectedList");
                if (arg2.getBooleanExtra("isFinish", false)) {
                    Intent in = new Intent();
                    in.putExtra("selectedList", selectedList);
                    setResult(RESULT_OK, in);
                    finish();
                    overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
                }
            }
        }
    }

    @Override
    public void itemClick(View v, Map<String, Object> item, int position) {
        if (v.getId() == R.id.view_click) {
            Intent in = new Intent(getApplicationContext(),
                    AddImgFragmentActivity02.class);
            in.putExtra("imageList",
                    Parse.getInstance().parseList(item.get("imageList")));
            in.putExtra("selectedList", selectedList);
            in.putExtra("maxImg", maxImg);
            startActivityForResult(in, RESULT_FIRST_USER);
            overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
        }
    }
}
